package com.axxc.cloud.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.axxc.cloud.mapper.ScBaseHotSearchKeywordMapper;
import com.axxc.cloud.pojo.ScBaseHotSearchKeyword;
import com.axxc.cloud.pojo.ScBaseHotSearchKeywordExample;
import com.axxc.cloud.pojo.ScBaseHotSearchKeywordExample.Criteria;
import com.axxc.cloud.service.IScBaseHotSearchKeywordService;

/**
 * @ClassName: ScBaseHotSearchKeywordServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author xxc
 * @date 2018年9月14日 下午3:10:42
 * @see {@link https://gitee.com/biuabiu }
 */
@Service
@Transactional
public class ScBaseHotSearchKeywordServiceImpl implements IScBaseHotSearchKeywordService {

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	private ScBaseHotSearchKeywordMapper searchKeywordMapper;

	@Override
	public int countByExample(ScBaseHotSearchKeywordExample example) {
		return this.searchKeywordMapper.countByExample(example);
	}

	@Override
	public int deleteByExample(ScBaseHotSearchKeywordExample example) {
		return this.searchKeywordMapper.deleteByExample(example);
	}

	@Override
	public Long saveOrUpdateEntity(Integer searchType, String keyword) {
		searchType = searchType == null ? -1 : searchType;
		if (StringUtils.isBlank(keyword) || searchType < -1 || searchType > 9) {
			return 0L;
		} else {
			// TODO 解决@@查询mycat分库数据库异常
			if (keyword.contains("@@")) {
				while (keyword.contains("@@")) {
					keyword = keyword.replaceAll("@@", "@");
				}
			}
			// 构造实体数据
			ScBaseHotSearchKeyword record = new ScBaseHotSearchKeyword();
			record.setKeyword(keyword);
			record.setSearchType(searchType);
			// 查询数据库是否存在该条记录
			ScBaseHotSearchKeywordExample example = new ScBaseHotSearchKeywordExample();
			Criteria criteria = example.createCriteria();
			criteria.andSearchTypeEqualTo(record.getSearchType());
			criteria.andKeywordEqualTo(record.getKeyword());
			List<ScBaseHotSearchKeyword> list = this.selectByExample(example);
			String currentTime = dateFormat.format(new Date());
			if (CollectionUtils.isEmpty(list)) {// 为空,不存在,新增一个
				record.setCreateTime(currentTime);
				record.setUpdateTime(currentTime);
				record.setSearchCount(0L);
				this.searchKeywordMapper.insertSelective(record);
			} else {// 存在,次数加一,并修改为可见状态
				record = list.get(0);
				record.setSearchCount(record.getSearchCount() + 1);
				record.setUpdateTime(currentTime);
				record.setIsVisible(1);// 存在可能屏蔽的时候,再搜索,故将此词状态可见
				this.updateByPrimaryKeySelective(record);
			}
			return record.getId();
		}
	}

	@Override
	public List<ScBaseHotSearchKeyword> selectByExample(ScBaseHotSearchKeywordExample example) {
		if (example != null && example.getPage() != null && example.getRow() != null) {
			Integer page = example.getPage() < 0 ? 0 : example.getPage();
			Integer row = example.getRow() < 0 ? 10 : example.getRow();
			example.setPage(page * row);// 数据库分页计算
			example.setRow(row);
		}
		return this.searchKeywordMapper.selectByExample(example);
	}

	@Override
	public ScBaseHotSearchKeyword selectByPrimaryKey(Long id) {
		return this.searchKeywordMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(ScBaseHotSearchKeyword record) {
		return this.searchKeywordMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public ScBaseHotSearchKeyword selectPropertiesByField(String propertyName, Object value) {
		return this.searchKeywordMapper.selectPropertiesByField(propertyName, value);
	}

	@Override
	public String selectPropertiesByPrimaryKey(Long id, Integer type) {
		if (type == 1) {
			return this.searchKeywordMapper.selectPropertiesByPrimaryKey(id);
		} else if (type == 2) {
			return this.searchKeywordMapper.selectPropertiesByPrimaryKey_2(id);
		}
		return this.searchKeywordMapper.selectPropertiesByPrimaryKey_2(id);
	}

}
